<?php

namespace app\admin\controller\sum;

use app\admin\model\sum\Slides as SlidesModel;
use app\admin\controller\sum\Common;
use think\Db;

/**
 * 轮播位置管理
 *
 * @icon fa fa-circle-o
 */
class Slideshows extends Common
{

    /**
     * Slideshows模型对象
     * @var \app\admin\model\sum\Slideshows
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sum\Slideshows;
        $this->model_slides = new \app\admin\model\sum\Slides;

    }



    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $list = $this->model->sheepFilter()->paginate($this->request->request('list_rows', 10));
        $this->success('', null, $list);
    }
    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $params = $this->request->only(['title', 'slug','slides']);
        $this->svalidate($params, '.add');
        $slides = json_decode($params['slides'], true);
        Db::transaction(function () use ($params, $slides) {
            $this->model->allowField(true)->save($params);
            $this->createOrEditSlide($slides, $this->model->id);
        });
        $this->success('保存成功', null, $this->model);
    }
    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        if (!$detail) {
            $this->error(__('No Results were found'));
        }
        $slides=$this->model_slides->where('slideshow_id',$id)->order('weigh', 'desc')->order('id', 'desc')->select();
        $return['slides']=$slides;
        $return['detail']=$detail;
        $this->success('获取成功', null, $return);
    }
    /**
     * 编辑(支持批量)
     */
    public function edit($id = null)
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch('add');
        }

        $params = $this->request->only(['title', 'slug','slides']);
        $slides = json_decode($params['slides'], true);
        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list, $params,$slides) {
            $count = 0;
            foreach ($list as $item) {
                $params['id'] = $item->id;
                $this->svalidate($params);
                $count += $item->allowField(true)->save($params);
                $this->createOrEditSlide($slides, $item->id);
            }
            return $count;
        });

        if ($result) {
            $this->success('更新成功', null, $result);
        } else {
            $this->error('更新失败，未改变任何记录');
        }
    }
    /**
     * 删除(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list) {
            $count = 0;
            foreach ($list as $item) {
                //删除对应轮播图
                SlidesModel::where('slideshow_id', $item->id)->delete();
                $count += $item->delete();
            }

            return $count;
        });

        if ($result) {
            $this->success('删除成功', null, $result);
        } else {
            $this->error(__('No rows were deleted'));
        }
    }

    /**
     * 添加修改轮播图
     * @param $slides
     * @param $slideshow_id
     * @return void
     */
    private function createOrEditSlide($slides, $slideshow_id)
    {
        foreach ($slides as $key => $data) {
            if (isset($data['id']) && $data['id']) {
                $slide = $this->model_slides->find($data['id']);
                if (!$slide) {
                    $this->error(__('No Results were found'));
                }
                if (isset($data['deleted']) && $data['deleted'] == 1) {
                    $slide->delete();
                } else {
                    $slide->slideshow_id = $slideshow_id;
                    $slide->title = $data['title'];
                    $slide->description = $data['description'];
                    $slide->image = $data['image'];
                    $slide->bg_image = $data['bg_image'];
                    $slide->status = $data['status'];
                    $slide->weigh = $data['weigh'];
                    $slide->save();
                }
            }else {
                if (!isset($data['deleted']) || !$data['deleted']) {
                    $slide = new SlidesModel();
                    $slide->slideshow_id = $slideshow_id;
                    $slide->title = $data['title'];
                    $slide->description = $data['description'];
                    $slide->image = $data['image'];
                    $slide->bg_image = $data['bg_image'];
                    $slide->status = $data['status'];
                    $slide->weigh = $data['weigh'];
                    $slide->save();
                }
            }
        }
    }

}
